package cn.elwy.rcp.ui.explorer.core.internal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.elwy.common.exception.RunException;

@SuppressWarnings("unchecked")
public class SpringContext {

	private static final Logger logger = LoggerFactory.getLogger(SpringContext.class);
	public static ApplicationContext applicatonContext;

	static {
		init();
	}

	private static void init() {
		try {
			applicatonContext = new ClassPathXmlApplicationContext("/spring/spring-context.xml");
			logger.info("Initialization springContext success");
		} catch (Throwable e) {
			String message = "Initialization springContext error:";
			logger.error(message, e);
			throw new RunException(message, e);
		}
	}

	private SpringContext() {
	}

	public static ApplicationContext getAppContext() {
		return applicatonContext;
	}

	public static <T> T getBean(String beanName) {
		return (T) applicatonContext.getBean(beanName);
	}

	public static <T> T getBean(Class<T> clazz) {
		return (T) applicatonContext.getBean(clazz);
	}

	public static <T> T getBean(String beanName, Class<T> clazz) {
		return (T) applicatonContext.getBean(beanName);
	}

}
